/*
 * ItemController.java
 *
 * Created on 26. Dezember 2006, 16:40
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.graphics.item;

import com.jme.animation.SpatialTransformer;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import kanjitori.map.Content;
import kanjitori.graphics.item.Item;
import kanjitori.map.ContentMatrix;
import kanjitori.map.Position;

/**
 *
 * @author Pirx
 */
public class ItemController extends Controller {
    
    private Item item;
    private Position pos;
    private ContentMatrix contentMatrix;
    
    
    /** Creates a new instance of ItemController 
     * @param item 
     * @param contentMatrix 
     */
    public ItemController(Item item, ContentMatrix contentMatrix) {
        this.item = item;
        this.contentMatrix = contentMatrix;
        pos = contentMatrix.findFreePos();
        item.getNode().setLocalTranslation(new Vector3f(2 * pos.x(), -0, 2 * pos.y()));
        contentMatrix.setContent(pos, Content.ITEM);
        SpatialTransformer st = new SpatialTransformer(1);
        st.setObject(item.getNode(), 0, -1);
        st.setRepeatType(Controller.RT_WRAP);
        item.getNode().addController(st);
        st.setPosition(0, 0f, new Vector3f(2 * pos.x(), -0.3f, 2 * pos.y()));
        st.setPosition(0, 2f, new Vector3f(2 * pos.x(), -0.1f, 2 * pos.y()));
        st.setPosition(0, 4f, new Vector3f(2 * pos.x(), -0.3f, 2 * pos.y()));

        Vector3f up = new Vector3f(0,1,0);
        Quaternion x0=new Quaternion();
        x0.fromAngleAxis(0, up);
        st.setRotation(0, 0f, x0);
        Quaternion x180=new Quaternion();
        x180.fromAngleAxis(FastMath.PI, up);
        st.setRotation(0, 2f, x180);
        Quaternion x360=new Quaternion();
        x360.fromAngleAxis(2*FastMath.PI, up);
        st.setRotation(0, 4f,x360); 
        
        st.setNewAnimationTimes(0, 4);
        st.setCurTime(0);
        st.interpolateMissing();
    }

    public void update(float f) {
        if (item.checkPlayerCollision()) {
            contentMatrix.setContent(pos, Content.EMPTY);
            item.consume();
        }
    }
    
}
